package core.image;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Image {
	
	private BufferedImage image;
	
	private int rotCenterX = 0;
	private int rotCenterY = 0;
	private double rotAngle = 0;

	private int scaCenterX = 0;
	private int scaCenterY = 0;
	private double scaleX = 1;
	private double scaleY = 1;
	
	public Image(BufferedImage img){
		image = img;
	}
	
	public Image(String location){
		try {
			image = ImageIO.read(new File(location));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void draw(Graphics2D g){
		draw(g, 0, 0);
	}
	
	/**
	 * Translate and draw the image
	 */
	public void draw(Graphics2D g, int x, int y){
		if(image != null){
			AffineTransform xform = new AffineTransform();
			double newScaX = scaCenterX * scaleX;
			double newScaY = scaCenterY * scaleY;
			xform.translate(x, y);
			xform.translate((scaCenterX - newScaX), (scaCenterY - newScaY));
			xform.scale(scaleX, scaleY);
			xform.rotate(rotAngle, rotCenterX, rotCenterY);
			g.drawImage(image, xform, null);
		}
	}
	
	public void setRotationCenter(int x, int y){
		rotCenterX = x;
		rotCenterY = y;
	}
	
	public void setRotationAngle(double angle){
		rotAngle = angle;
	}
	
	public void setScaleFactor(double sx, double sy){
		scaleX = sx;
		scaleY = sy;
	}
	
	public void setScaleCenter(int x, int y){
		scaCenterX = x;
		scaCenterY = y;
	}
	
	public int getWidth(){
		return image.getWidth();
	}
	
	public int getHeight(){
		return image.getHeight();
	}
	
	public BufferedImage getImage(){
		return image;
	}
	

}
